package com.agregatesdk.point;

import com.agregatesdk.core.Constant;
import com.agregatesdk.core.HttpUtil;
import com.agregatesdk.core.SDKListView;
import com.agregatesdk.core.UserAccount;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class PointManager {

    public static float dealWithCredit(final Activity context, int credit, int offerType) {
        SharedPreferences config = context.getSharedPreferences(Constant.CONFIG_NAME, 0);
        int total = config.getInt(String.valueOf(offerType), 0);
        config.edit().putInt(String.valueOf(offerType), total + credit).commit();

        if (credit > 100) {
            credit = 50;
        }

        context.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                SDKListView.updateTaskTips(getUserTotalCredit(context), Float.valueOf(UserAccount.getInstance().getRest()));
            }
        });

        return (float) (credit * 0.3);
    }

    public static float getUserTotalCredit(Activity context) {
        SharedPreferences config = context.getSharedPreferences(Constant.CONFIG_NAME, 0);

        int wapsCredit = config.getInt(String.valueOf(Constant.SDK_TYPE_WAPS), 0);
        int midiiCredit = config.getInt(String.valueOf(Constant.SDK_TYPE_MIDII), 0);
        int dianjinCredit = config.getInt(String.valueOf(Constant.SDK_TYPE_DIANJIN), 0);
        int yijifenCredit = config.getInt(String.valueOf(Constant.SDK_TYPE_QUMI), 0);

        return (float) ((wapsCredit + midiiCredit + dianjinCredit + yijifenCredit) * 0.3);
    }

    public static void sendCredit2Server(final Activity context, final float credit, final int offerType) {
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    if (HttpUtil.addCredit4User(context.getPackageName(), context, credit, offerType)) {
						context.runOnUiThread(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								Toast.makeText(context, offerType + "发财猪：恭喜您赚的猪猪币:" + credit, Toast.LENGTH_LONG).show();
							}
						});
                    }
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();
    }

}
